const app = getApp()
const api = require('../../config/api.js'); 
const util = require('../../utils/util.js'); 
const hub2 = require('../../utils/Hub2.js');
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    options: '',
    provinces: ["京", "沪", "鄂", "湘", "川", "渝", "粤", "闽", "晋", "黑", "津", "浙", "豫", "赣", "贵", "青", "琼", "宁", "吉", "蒙", "冀", "苏", "皖", "桂", "云", "陕", "甘", "藏", "新", "辽", "鲁"],
    keyNums: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Q", "W", "E", "R", "T", "Y", "U", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M", "港", "澳", "学"],
    type: ['蓝牌', '新能源'],
    typeId: 0,
    isShow: false, //弹窗显示隐藏
    showList: [], //键盘列表
    showId: '', //键盘ID 1-省份 2-数字
    name: '', //省份
    nums: ['', '', '', '', '', ''], //数字
    
    isVipUser: false,
    isVipStation: false,
    sureName: '',
    de_btn: '--color: #08a3e9;',
    head: '--color: #75c7ed;',
    b_backimg: '../../img/KC01.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     if(app.TenantName!=='与好智行')
     {
         this.setData({de_btn: '--color: #fe8d04;',head: '--color: #fcc682;',b_backimg: '../../img/KC02.png'});
     }
     if(app.VipParams!=null)
     {
          this.setData({
             isVipStation: true
          })
     }
     if(app.NewVip!=null)
     {
          if(app.PhoneNumber==""  && app.VipParams!=null){
            util.showErrorToast("请授权获取您的手机号码方可注册",999999); 
            return;
          }
          this.setData({
             isVipUser: true,
             sureName: app.NewVip.member_surname
          })
     }
 },
     
 /**车牌界面部分 */
 ok(){
  this.setData({
    isPop:false
  })
}, 
input(e){
   this.setData({sureName: e.detail.value})
},
search() { //查询
  let nums = this.data.nums;
  nums = nums.join('');
  let data_nums = this.data.name + nums;
  if ((this.data.name!="无" && (this.data.name == '' || this.data.nums[this.data.nums.length - 1] == ''))) {
    wx.showToast({
      title: '请输入完整车辆',
      icon: 'none'
    });
    return false;
  }
  
  let PlateNo = this.data.name+this.data.nums.join('');
  let sureName = this.data.sureName;
  let phone = "";
  let vipCode = "";
  if(this.data.isVipStation){
      if(app.NewVip!=null){
          phone = app.NewVip.telephone;
          vipCode = app.NewVip.member_code;
      }
      else
      {
          if(app.PhoneNumber && app.PhoneNumber!=null && app.PhoneNumber!="")
          {
              phone = app.PhoneNumber;
          }
      }
  } 
 
  //开始绑定
  util.request2(api.bindCar, {  
    TenantId: app.TenantId,
    StationId: app.StationId,
    PlateNo: PlateNo,
    sureName: sureName,
    OpenId: app.OpenId,
    Phone: phone,
    VipCode: vipCode
  }, 'POST').then(function(res){
        if (res.startsWith('*')) {    
            //注册成功
            util.tryHideFullScreenLoading(true);
            util.showErrorToast(res.replace('*',''),5000);
            //发送一次算费请求
            hub2.GetSationId(PlateNo, 4, 0, -99, app.payEndTime, app.fromOutGate);//算费请求
            wx.redirectTo({
              url: '/pages/index/index',
            })
        }  
        else {   
            //发生错误
            util.tryHideFullScreenLoading(true);
            util.showErrorToast(res,5000);
        }
    },
    function(errmsg){  
      util.tryHideFullScreenLoading(true);
      util.showErrorToast(errmsg,999999);
    }
  ); 
 },
clear() { //清除
  let flag = '';
  let nums = this.data.nums;
  if (nums[0] != '') { //清除数字键盘
    for (let i in nums) {
      if (nums[i] != '') {
        flag = i
      }
    }
    nums[flag] = '';
    this.setData({
      nums: nums
    });
  } else { //清除省份键盘
    if (this.data.typeId != 2) {
      this.setData({
        name: '',
        showList: this.data.provinces, //键盘列表
        showId: 1, //键盘ID 1-省份 2-数字
      });
    }
  }
},
setName(e) { //设置内容
  if (this.data.showId == 1) { //设置身份
    this.setData({
      name: e.currentTarget.dataset.name
    });
    this.setData({ //输入身份键盘切换为数字
      showId: 2,
      showList: this.data.keyNums
    })
  } else { //设置数字
    let nums = this.data.nums;
    for (let i in nums) {
      if (nums[i] == '') {
        nums[i] = e.currentTarget.dataset.name;
        break;
      }
    }
    this.setData({
      nums: nums
    })
  }
},
setIsShow(e) { //弹窗显示隐藏
  let id = this.data.typeId == 2 ? 2 : (e.currentTarget.dataset.id ? e.currentTarget.dataset.id : ''); //1-省份 2-数字
  if (id == '' && this.data.nums[this.data.nums.length - 1] == '') {
    wx.showToast({
      title: '请输入完整车辆',
      icon: 'none'
    });
    return false
  }
  if (id == 2 && this.data.name == '') {
    if (this.data.typeId == 2) {
      return false
    }
    this.setData({
      isShow: id != '' ? true : !this.data.isShow,
      showId: 1,
      showList: this.data.provinces
    })
  } else {
    this.setData({
      isShow: id != '' ? true : !this.data.isShow,
      showId: id,
      showList: id == 1 ? this.data.provinces : (id == 2 ? this.data.keyNums : [])
    })
  }
},
submit() {
  if (this.data.nums[this.data.nums.length - 1] == '') {
    wx.showToast({
      title: '请输入完整车辆',
      icon: 'none'
    });
    return false
  }
  this.setData({
    isShow: false
  });
},
setType(e) { //设置类型 0-蓝牌 1-新能源 2-无牌
  console.log(e)
  this.setData({
    typeId: e.currentTarget.dataset.id,
    isShow: false, //弹窗显示隐藏
    showList: [], //键盘列表
    showId: '', //键盘ID 1-省份 2-数字
    name: e.currentTarget.dataset.id == 2 ? '无' : '', //省份
    nums: e.currentTarget.dataset.id == 1 ? ['', '', '', '', '', '', ''] : ['', '', '', '', '', ''], //数字
  });
 
},
})